package com.dgcsxy.my_college_note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgcsxy.my_college_note.common.utils.SnowFlakeIdWorker;
import com.dgcsxy.my_college_note.entity.dto.log.LogInfoDto;
import com.dgcsxy.my_college_note.entity.pojo.McnLog;
import com.dgcsxy.my_college_note.entity.vo.log.LogAddVo;
import com.dgcsxy.my_college_note.entity.vo.log.LogUpdateVo;
import com.dgcsxy.my_college_note.mapper.McnLogMapper;
import com.dgcsxy.my_college_note.service.McnLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-10-24
 */
@Service
public class McnLogServiceImpl extends ServiceImpl<McnLogMapper, McnLog> implements McnLogService {

    private final McnLogMapper mcnLogMapper;
    private final SnowFlakeIdWorker snowFlakeIdWorker;

    public McnLogServiceImpl(McnLogMapper mcnLogMapper, SnowFlakeIdWorker snowFlakeIdWorker) {
        this.mcnLogMapper = mcnLogMapper;
        this.snowFlakeIdWorker = snowFlakeIdWorker;
    }

    @Override
    public String addLog(LogAddVo logAddVo) {
        McnLog mcnLog = new McnLog();
        mcnLog.setLogId(String.valueOf(snowFlakeIdWorker.nextId()));
        mcnLog.setUserId(logAddVo.getUserId());
        mcnLog.setLogTime(LocalDateTime.now());
        mcnLog.setLogTitle(logAddVo.getLogTitle());
        mcnLog.setLogContent(logAddVo.getLogContent());
        mcnLog.setLogPicture(logAddVo.getLogPicture());
        mcnLog.setLogLocation(logAddVo.getLogLocation());
        mcnLog.setLogStatus(1);
        int insert = mcnLogMapper.insert(mcnLog);
        return insert > 0 ? "插入成功" : "插入失败";
    }

    @Override
    public String deleteLog(String logId) {
        McnLog mcnLog = new McnLog();
        mcnLog.setLogId(logId);
        mcnLog.setLogStatus(0);
        int result = mcnLogMapper.update(mcnLog, new QueryWrapper<McnLog>()
                .eq("log_id", mcnLog.getLogId()));
        return result > 0 ? "删除成功" : "删除失败，无此日记";
    }

    @Override
    public String updateLog(LogUpdateVo logUpdateVo) {
        McnLog mcnLog = new McnLog();
        mcnLog.setLogId(logUpdateVo.getLogId());
        mcnLog.setLogTitle(logUpdateVo.getLogTitle());
        mcnLog.setLogContent(logUpdateVo.getLogContent());
        mcnLog.setLogPicture(logUpdateVo.getLogPicture());
        int result = mcnLogMapper.update(mcnLog, new QueryWrapper<McnLog>()
                .eq("log_id", mcnLog.getLogId()));
        return result > 0 ? "修改成功" : "修改失败";
    }

    @Override
    public LogInfoDto selectLog(String logId) {
        McnLog mcnLog = mcnLogMapper.selectOne(new QueryWrapper<McnLog>()
                .eq("log_id", logId)
                .eq("log_status",1));
        if(mcnLog==null){
            return null;
        }
        LogInfoDto logInfoDto = new LogInfoDto();
        logInfoDto.setLogId(mcnLog.getLogId());
        logInfoDto.setUserId(mcnLog.getUserId());
        logInfoDto.setLogTime(mcnLog.getLogTime());
        logInfoDto.setLogTitle(mcnLog.getLogTitle());
        logInfoDto.setLogPicture(mcnLog.getLogPicture());
        logInfoDto.setLogLocation(mcnLog.getLogLocation());
        return logInfoDto;
    }

    @Override
    public List<McnLog> selectAllLog(String userId) {
        List<McnLog> mcnLogs = mcnLogMapper.selectList(new QueryWrapper<McnLog>()
                .eq("user_id", userId)
                .eq("log_status", 1)
                .orderByDesc("log_time")
        );
        return mcnLogs;
    }
}
